package com.quzhi.sys.controller.UserController;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quzhi.common.Result;
import com.quzhi.sys.entity.Role;
import com.quzhi.sys.entity.RoleMenu;
import com.quzhi.sys.entity.UserRole;
import com.quzhi.sys.service.impl.RoleMenuServiceImpl;
import com.quzhi.sys.service.impl.RoleServiceImpl;
import com.quzhi.sys.service.impl.UserRoleServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;
import java.util.*;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author Xibing
 * @since 2024-07-16
 */
@RestController
@RequestMapping("/sys/role")
public class RoleController {

    @Autowired
    private RoleServiceImpl roleService;

    @Autowired
    private UserRoleServiceImpl userRoleService;


    @Autowired
    private RoleMenuServiceImpl roleMenuService;

    // 分页查询所有角色

    @GetMapping("/getRoles")
    public Result<?> getRoles(@RequestParam(value = "roleName", required = false) String roleName,
                              @RequestParam(value = "pageNo") Long pageNo,
                              @RequestParam(value = "pageSize") Long pageSize) {

        LambdaQueryWrapper<Role> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.hasLength(roleName), Role::getRoleDesc, roleName).or().eq(StringUtils.hasLength(roleName), Role::getRoleName, roleName);
        wrapper.ne(Role::getRoleName, "admin");

        Page<Role> infoPage = new Page<>(pageNo, pageSize);
        roleService.page(infoPage, wrapper);

        Map<String, Object> rows = new HashMap<>();
        rows.put("rows", infoPage.getRecords());
        rows.put("total", infoPage.getTotal());

        return Result.success(rows);
    }

    /**
     * 获取所有权限集合
     */
    @GetMapping("/list")
    public Result<?> getRolesList() {
        // 不返回超级管理员
        List<Role> reData = new ArrayList<>();
        List<Role> list = roleService.list();

        for (Role role : list) {
            if (!role.getRoleName().equals("admin")) {
                reData.add(role);
            }
        }

        return Result.success(reData);
    }


    /**
     * 查询用户
     */
    @GetMapping("/{id}")
    public Result<?> getRoleById(@PathVariable String id) {

        return Result.success(roleService.getById(id));
    }


    /**
     * 新增角色
     */
    @PostMapping
    public Result<?> addRole(@RequestBody Role role) {
        role.setRoleId(UUID.randomUUID().toString());

        roleService.save(role);

        return Result.success("新增成功");
    }


    /**
     * 修改角色
     */
    @PutMapping
    public Result<?> updateRolesById(@RequestBody Role role) {

        roleService.updateById(role);

        return Result.success("修改成功");
    }


    /**
     * 删除角色
     */
    @DeleteMapping("/{id}")
    public Result<?> deleteById(@PathVariable String id) {

        // 删除用户绑定的角色
        // 查询用户绑定的角色
        LambdaQueryWrapper<UserRole> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserRole::getRoleId, id);
        userRoleService.remove(wrapper);

        // 删除角色绑定的路由
        LambdaQueryWrapper<RoleMenu> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(RoleMenu::getRoleId, id);
        roleMenuService.remove(lambdaQueryWrapper);


        roleService.removeById(id);

        return Result.success("删除成功");
    }
}
